﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Windows.Controls;

namespace SkeinforgeWP8
{
    class BoxStream : Stream
    {
        MainPage main;

        public BoxStream(MainPage _main)
        {
            Debug.WriteLine("Writing stream to box");

            main = _main;
        }

        public override void WriteByte(byte value)
        {
            base.WriteByte(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string newText = Encoding.UTF8.GetString(buffer, offset, count).ToString();
            //Debug.WriteLine(newText);
            try
            {
                main.DispatchInvoke(() =>
                {
                    main.OutBox.Text += newText;
                    main.Scroller.UpdateLayout();
                    main.Scroller.ScrollToVerticalOffset(main.Scroller.ScrollableHeight);
                    main.Scroller.UpdateLayout();
                });
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void Flush()
        {
            return;
        }

        public override long Position
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override long Length
        {
            get { throw new NotImplementedException(); }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return false; }
        }

        public override bool CanRead
        {
            get { return false; }
        }
    }
}
